package com.tokenplus.thai.me

import android.annotation.SuppressLint
import android.os.Handler
import android.os.Looper
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebResourceRequest
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.lifecycle.ViewModel
import com.tokenplus.thai.base.BaseActivity
import com.tokenplus.thai.databinding.ActivityAboutUsWebViewBinding
import com.tokenplus.thai.enums.PageTagEnum
import com.tokenplus.thai.extend.*
import com.tokenplus.thai.webview.AppJavaScriptImplement
import com.tokenplus.thai.webview.AppJavaScriptInterface


/** webview页 */
@SuppressLint("CustomSplashScreen")
class AboutUsWebViewActivity : BaseActivity(), AppJavaScriptImplement {

    //布局binding
    private lateinit var mActivityBinding: ActivityAboutUsWebViewBinding

    override fun onBindingViewModel(): List<ViewModel> {
        val list = mutableListOf<ViewModel>()
        return list
    }

    override fun onBindingViewTag(): String {
        return PageTagEnum.app_web_view.name.uppercase()
    }

    override fun onBindingContentView(): View {
        mActivityBinding =
            ActivityAboutUsWebViewBinding.inflate(layoutInflater)
        return mActivityBinding.root
    }

    @SuppressLint("SetJavaScriptEnabled")
    override fun onContentViewBinded() {
        try {
            //清理缓存
            mActivityBinding.webAppWebView.clearCache(true);
            mActivityBinding.webAppWebView.clearHistory();
            mActivityBinding.webAppWebView.clearFormData();

            mActivityBinding.webAppWebView.webChromeClient = mWebChromeClient
            mActivityBinding.webAppWebView.webViewClient = mWebClient
            val webSettings: WebSettings = mActivityBinding.webAppWebView.settings
            try {
                webSettings.javaScriptEnabled = true
                webSettings.textZoom = 100
                webSettings.domStorageEnabled = true
                webSettings.builtInZoomControls = true
                webSettings.useWideViewPort = true
                webSettings.loadWithOverviewMode = true
                webSettings.cacheMode = WebSettings.LOAD_NO_CACHE
                webSettings.layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN
                webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW)
                webSettings.allowUniversalAccessFromFileURLs = true
                webSettings.allowFileAccessFromFileURLs = true
                webSettings.allowContentAccess = true
            } catch (e: Exception) {
                e.printStackTrace()
            }

            // 创建一个 Java 对象，它将注入到 H5 页面中
            val appJavaScriptInterface = AppJavaScriptInterface(this)
            mActivityBinding.webAppWebView.addJavascriptInterface(appJavaScriptInterface, "android")

            //加载网页
            val url = "https://www.tokenplusapp.com/thailand/aboutus"

            Handler(Looper.getMainLooper()).postDelayed({
                mActivityBinding.webAppWebView.loadUrl(url)
            }, 1000)

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    private val mWebChromeClient = object : WebChromeClient() {
        override fun onProgressChanged(view: WebView?, newProgress: Int) {
            super.onProgressChanged(view, newProgress)

            mActivityBinding.progressBar.progress = newProgress
            if (newProgress == 100) {
                mActivityBinding.progressBar.visibility = View.GONE
            } else {
                mActivityBinding.progressBar.visibility = View.VISIBLE
            }
        }
    }

    private val mWebClient = object : WebViewClient() {
        override fun shouldOverrideUrlLoading(
            view: WebView?,
            request: WebResourceRequest?,
        ): Boolean {
            return shouldOverrideUrlLoading(view, request!!.url.toString())
        }

        override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
            return false
        }
    }

    override fun queryUserState(msg: String) {
        println("======== queryUserState $msg")
    }

    override fun openSysBrowser(msg: String) {
        println("======== openSysBrowser $msg")
    }

    override fun toHomeActivity(msg: String) {
        println("======== toHomeActivity $msg")
    }
}